import { JJUtil, JJLog } from "../util/index.js";

export default class JJHtmlHelper {

    static getHtmlBody(str) {
        if (JJUtil.isNull(str)) {
            return '';
        }
        const bodyStart = '<body>';
        const bodyEnd = '</body>';
        let array1 = str.split(bodyStart);
        if (array1.length > 1) {
            let array2 = array1[1].split(bodyEnd);
            return array2[0];
        } else {
            return str;
        }
    }

    static replaceHtmlHead(str) {
        if (JJUtil.isNull(str)) {
            return '';
        }
        const bodyStart = '<body>';
        const bodyEnd = '</body>';
        const headString = '<html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" /><style>p{margin:10px}</style></head><body>';
        let array1 = str.split(bodyStart);
        if (array1.length > 1) {
            let array2 = array1[1].split(bodyEnd);
            return headString + array2[0] + '</body></html>'
        } else {
            return str;
        }
    }

    static parseHTMLAndTransformIntoArray(html) {
        let htmlStr = htmlStr = html.replace(/[ ]+/g, ''); // 空格 和 &nbsp;
        htmlStr = htmlStr.replace(/[a-zA-Z]+[-]?[a-zA-Z]+:[a-zA-Z0-9]+;/g, ''); // value: 数字(浮点)、英文字母
        htmlStr = htmlStr.replace(/[a-zA-Z]+[-]?[a-zA-Z]+:[0-9]+\.?[a-zA-Z0-9]+;/g, ''); // value: 数字(浮点)、英文字母
        htmlStr = htmlStr.replace(/[a-zA-Z]+[-]?[a-zA-Z]+:[0-9]+%/g, ''); // value: 数字 和 %
        htmlStr = htmlStr.replace(/[a-zA-Z]+[-]?[a-zA-Z]+:[rgbRGB]+\([0-9]+,[0-9]+,[0-9]+\)/g, ''); 
        htmlStr = htmlStr.replace(/[a-zA-Z]+[-]?[a-zA-Z]+:[a-zA-Z]+[,]?[a-zA-Z]+/g, ''); // value: 英文,英文
        htmlStr = htmlStr.replace(/[a-zA-Z]+[-]?[a-zA-Z]+:[\u4e00-\u9fa5]+,[a-zA-Z]+/g, ''); // value: 汉字,英文
        htmlStr = htmlStr.replace(/[a-zA-Z]+[-]?[a-zA-Z]+:[a-zA-Z]+,[\u4e00-\u9fa5]+/g, ''); // value: 英文,汉字
        htmlStr = htmlStr.replace(/[a-zA-Z]+[-]?[a-zA-Z]+:[\u4e00-\u9fa5]+[,]?[\u4e00-\u9fa5]+/g, ''); // value: 英文,汉字
        htmlStr = htmlStr.replace(/[a-zA-Z]+[-]?[a-zA-Z]+:[\u4e00-\u9fa5]+/g, ''); // value: 汉字
        htmlStr = htmlStr.replace(/style="[;]+"/g, '')
        htmlStr = htmlStr.replace(/style=""/g, '') // 移除 style 标签
        htmlStr = htmlStr.replace(/id="[a-zA-Z0-9]+"/g, '') // 移除 id 标签
        htmlStr = htmlStr.replace(/class="[a-zA-Z0-9]+"/g, '')
        htmlStr = htmlStr.replace(/<\/?span>/g, '') // 移除 span 标签
        htmlStr = htmlStr.replace(/<\/?strong>/g, '') //  移除 strong 标签
        htmlStr = htmlStr.replace(/<\/p>/g, '') //  移除 </p> 
        htmlStr = htmlStr.replace(/<br\/>/g, '') //  移除 br 标签
        htmlStr = htmlStr.replace(/<spanstyle="font-family:&quot;TimesNewRoman&quot;;">/g, ''); // <spanstyle="font-family:&quot;TimesNewRoman&quot;;">
        let result = htmlStr.indexOf('&nbsp;') != -1
        while (result) {
            htmlStr = htmlStr.replace('&nbsp;', '')
            result = htmlStr.indexOf('&nbsp;') != -1
        }
        let pArray = htmlStr.split("<p>");
        JJLog.log('协议html解析：' + JSON.stringify(pArray));
        return pArray;
    }
}
